home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Tradition or what?
- Date: Sun, 25 Feb 96 22:58:00 GMT
- Organization: none
- Message-ID: <825289080snz@genesis.demon.co.uk>
- References: <4g0elg$mdr@redstone.interpath.net> <Dn4wML.5LJ@undergrad.math.uwaterloo.ca> <824950522snz@genesis.demon.co.uk> <4gqdop$jfo@redstone.interpath.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gqdop$jfo@redstone.interpath.net>
- softbase@mercury.interpath.net
- "Scott McMahan - Softbase Systems" writes:
-
- >Speaking of bizarre return values, do people realize 0 is used to mean
- >both success, failure, and a valid return value in the standard
- >library!? All three in the same library!
-
- I don't find that to be a great problem. The semantics of each function
- usually suggest quite naturally what 0 should mean.
-
- >0 means success when an error code is returned. clock() is an
- >example -- since -1 means error, any other value is successful.
-
- Right - when you consider what clock() does then 0 is a natural value
- for a successful operation.
-
- >(I need to go back and check, but I think this is how it works.)
- >
- >0 means failure when a valid value is returned like a pointer from
- >malloc().
-
- Functions that return pointers cannot return 0 - they return null pointers.
- Null pointers are routinely used to indicate failure or the absence of
- a value.
-
- >Zero is also a valid value in strcmp which can't fail (unless you pass
- >it bogus strings and it crashes) -- 0 means equal from the set of less
- >than, equal, and greater than.
-
- strcmp probably generates the greatest amount of discussion on this topic.
- However the way it is defined allows it to be implemented very efficiently
- on most archetectures and makes a lot of sense if you think of it in
- similar terms to subtraction (or difference) in maths. Algebraically
- a-b is negative is a<b, 0 if a==b and positive if a>b. That is also why
- it implements efficiently.
-
- >No wonder C is confusing and error prone! Try defining a macro
- >to represent what 0 means in the stdlib! :)
-
- There's no point in doing this - 0 is 0.
-
- >I think this is the case against explicit tests against 0 in
- >if, while, etc expressions which decide what to do based on the
- >return value of a function. It's much more clear if there's no
- >explicit test against 0.
-
- This statement seems pretty much unrelated to what you wrote previously.
- How is implicit comparison with 0 clearer than explicit comparison with 0.
- What you say above seems to indicate that explicit comparison with NULL,
- is the clearest approach where pointers results are concerned.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-